package com.yunhe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhe.common.QueryPageParam;
import com.yunhe.common.Result;
import com.yunhe.entity.Goods;
import com.yunhe.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
@RestController
@RequestMapping("/goods")
@Api(value = "/goods",description = "物品信息管理")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    /**
     *物品信息保存
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("物品信息保存")
    public Result save(
            @Param("物品信息")
            @RequestBody Goods goods){
        return goodsService.save(goods)?Result.success():Result.fail();
    }

    /**
     *物品信息更新;update--put倒过去
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("物品信息更新")
    public Result update(
            @Param("物品信息")
            @RequestBody Goods goods){
        return goodsService.updateById(goods)?Result.success():Result.fail();
    }

    @GetMapping("/del")
    @ApiOperation("根据物品id来删除物品")
    @ApiImplicitParam(paramType = "query",dataType = "Long",name ="id",value = "物品编号",required = true)
    public Result del(Long id){
            return goodsService.removeById(id)?Result.success():Result.fail();
    }

    @PostMapping("/listPage")
    @ApiOperation("分页查询和模糊查询")
    public Result listPage(@RequestBody QueryPageParam query){
        //得到传递的数据hahmap对象,param
        HashMap param = query.getParam("param");
        String name= (String) param.get("name");
        String goodstype= (String) param.get("goodstype");
        String storage= (String) param.get("storage");


        //分页对象,创建，且赋值;
        Page<Goods>page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        //创建查询包装器
        LambdaQueryWrapper<Goods>queryWrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name)&&!"null".equals(name))
            queryWrapper.like(Goods::getName, name);
        //其他两个校验暂时不写
        //调用业务层的page()方法
        Page<Goods> result = goodsService.page(page, queryWrapper);

        return Result.success(result.getRecords(), result.getTotal());
    }

}